#include "../include/PageLib.hpp"
#include "../include/TF_IDF.hpp"
#include "../include/InvertIndexTable.h"

using std::ofstream;

InvertedIndex::InvertedIndex(const PageLib & pagelib, Deduplication & wordseg, TF_IDF & tfidf)
: _ripepage(pagelib.get_ripepage())
, _offset(pagelib.get_offset())
, _confMap(pagelib.get_confMap())
, _wordseg(wordseg)
, _tfidf(tfidf)
{

}

InvertedIndex::~InvertedIndex()
{

}

void InvertedIndex::create_wordmap()
{
    int docid = 1;
    for(auto & rssitem : _ripepage)
    {

        vector<string> results = _wordseg.wordSeg(rssitem.content, 10000);
        _wordnum.insert(std::make_pair(docid, results.size()));
        for(auto & res : results)
        {
            auto it = _wordmap.find(res);
            if(it == _wordmap.end())
            {
                map<int,int> submap = {std::make_pair(docid, 1)};
                _wordmap[res] = submap;
            }
            else
            {
                auto it2 = _wordmap[res].find(docid);
                if(it2 == _wordmap[res].end())
                {
                    _wordmap[res].insert(std::make_pair(docid,1));
                }
                else
                {
                    ++(_wordmap[res][docid]);
                }
            }
        }
        ++docid;

    }
}

void InvertedIndex::create_invertedTable()
{
    _invertedTable = _tfidf(_wordmap, _wordnum);
}

void InvertedIndex::store()
{
    ofstream ofs(_confMap["invertIndex"]);
    if (!ofs)
    {
        std::cout << "ofstream open file " << _confMap["invertIndex"] << " error!\n";
        std::cout <<" InvertedIndex::store error !"<<std ::endl;
        return;
    }
    for (auto &invert : _invertedTable)
    {
        ofs << invert.first;
        for (auto sub : invert.second)
        {
            ofs << " " << sub.first << " " << sub.second;
        }
        ofs << std::endl;
    }
    ofs.close();
}

